package com.community.controller;

import com.community.pojo.Announcement;
import com.community.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class AnnouncementController {
    private AnnouncementService announcementService;
    @Autowired
    public void setAnnouncementService(AnnouncementService announcementService) {
        this.announcementService = announcementService;
    }

    @GetMapping("/find-announcement-form")
    public String findAnnouncementFormController() {
        return "findAnnouncementForm";
    }
    @RequestMapping("/find-announcement")
    public String findAnnouncementController(@RequestParam String announcementId, Model model) {
        if(announcementId.equals("")){
            model.addAttribute("message","公告id不能为空!");
            return "findAnnouncementForm";
        }
        Integer id = Integer.parseInt(announcementId);
        Announcement announcement = announcementService.findAnnouncementById(id);
        if(announcement==null){
            model.addAttribute("message","该id的公告不存在!");
            return "findAnnouncementForm";
        }
        model.addAttribute("announcement",announcement);
        return "findAnnouncementResult";
    }
    @GetMapping("/find-all-announcement")
    public String findAllAnnouncementController(Model model) {
        List<Announcement> announcementList = announcementService.findAllAnnouncement();
        if(announcementList.size()==0){
            model.addAttribute("message","公告列表为空!");
        }else{
            model.addAttribute("announcementList",announcementList);
        }
        return "findAllAnnouncementResult";
    }


    @GetMapping("/add-announcement-form")
    public String addAnnouncementFormController() {
        return "addAnnouncementForm";
    }
    @PostMapping("/add-announcement")
    public String addAnnouncementController(@ModelAttribute Announcement announcement, Model model) {
        if(announcement.getTitle().equals("")||announcement.getContent().equals("")){
            model.addAttribute("message","不允许出现空值!");
            return "addAnnouncementForm";
        }
        int n= announcementService.addAnnouncement(announcement);
        if(n==1){
            model.addAttribute("message", "添加成功，新添加的公告id为: " + announcement.getAnnouncementId());
        }else{
            model.addAttribute("message","添加失败!");
        }
        return "addAnnouncementResult";
    }

    @GetMapping("/delete-announcement-form")
    public String deleteAnnouncementFormController() {
        return "deleteAnnouncementForm";
    }
    @PostMapping("delete-announcement")
    public String deleteAnnouncementController(@RequestParam String announcementId, Model model) {
        if(announcementId.equals("")){
            model.addAttribute("message","请输入要删除的公告id!");
            return "deleteAnnouncementForm";
        }
        Integer id=Integer.parseInt(announcementId);
        Announcement announcement = announcementService.findAnnouncementById(id);
        if(announcement==null){
            model.addAttribute("message", "用户id为: "+id+"的公告不存在，无法删除！" );
            return "deleteAnnouncementForm";
        }
        int n= announcementService.deleteAnnouncementById(id);
        if(n==1){
            model.addAttribute("message","已成功删除id为: " + id+"的公告");
        }else{
            model.addAttribute("message","id为: " + id+"的公告不存在，无法删除!");
        }
        return "deleteAnnouncementResult";
    }

    @GetMapping("/update-announcement-form-input-id")
    public String updateAnnouncementFormInputController( Model model) {
        return "updateAnnouncementFormInputId";
    }
    @PostMapping("/update-announcement-form")
    public String updateAnnouncementFormController(@RequestParam String announcementId, Model model) {
        if(announcementId.equals("")){
            model.addAttribute("message","请输入要修改的公告id!");
            return "updateAnnouncementFormInputId";
        }
        Integer id=Integer.parseInt(announcementId);
        Announcement announcement = announcementService.findAnnouncementById(id);
        if(announcement==null){
            model.addAttribute("message","id为: " + id+"的公告不存在，无法修改!");
            return "updateAnnouncementFormInputId";
        }
        model.addAttribute("announcement",announcement);
        return "updateAnnouncementForm";
    }
    @PostMapping("/update-announcement")
    public String updateAnnouncementController(@ModelAttribute Announcement announcement, Model model) {
        if(announcement.getTitle().equals("")||announcement.getContent().equals("")){
            model.addAttribute("message","不允许出现空值!");
            return "updateAnnouncementForm";
        }
        int n= announcementService.updateAnnouncement(announcement);
        if(n==1){
            model.addAttribute("message","修改成功!");
            Announcement findAnnouncement = announcementService.findAnnouncementById(announcement.getAnnouncementId());
            model.addAttribute("announcement",findAnnouncement);
            return "findAnnouncementResult";
        }else{
            model.addAttribute("message","修改失败!");
            return "updateAnnouncementForm";
        }
    }


}
